package com.example.hospital.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.hospital.pojo.*;
import com.example.hospital.service.IPatientService;
import com.example.hospital.service.IRsService;
import com.example.hospital.service.ISymptomService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@Api(tags = "SymptomController")
@RestController
@RequestMapping("/symptom")
public class SymptomController {
    @Autowired
    private ISymptomService iSymptomService;
    @Autowired
    private IPatientService iPatientService;
    @Autowired
    private IRsService iRsService;
    @ApiOperation(value = "添加症状")
    @PostMapping("/")
    public RespBean addSymptom(@RequestBody Symptom symptom){
        if(iSymptomService.save(symptom)){
            return RespBean.success("添加成功");
        }
        else {
            return RespBean.error("添加失败");
        }
    }


    @ApiOperation(value = "删除病症")
    @DeleteMapping("/{id}")
    public RespBean deleteSymptom(@PathVariable Integer id){
        if(iSymptomService.removeById(id)){
            return RespBean.success("删除成功");
        }
        else {
            return RespBean.error("删除失败");
        }
    }
    @ApiOperation(value = "修改病症")
    @PutMapping("/")
    public RespBean updateSymptom(@RequestBody Symptom symptom ){
        if(iSymptomService.updateById(symptom)){
            return RespBean.success("修改成功");
        }
        else {
            return RespBean.error("修改失败");
        }
    }
    @ApiOperation(value = "查询病症")
    @GetMapping("/")
    public List<Symptom> getSymptom(){
        return iSymptomService.list();
    }
    @ApiOperation(value = "分页查询病症")
    @GetMapping("/bypage")
    public ResPageBean getSymptomByPage(@RequestParam(defaultValue = "1") Integer currentPage,
                                      @RequestParam(defaultValue = "10")Integer size,@RequestParam(defaultValue = "") String keyword){
        return iSymptomService.getSymptomByPage(currentPage,size,keyword);
    }
    @ApiOperation(value = "根据id获取病症信息")
    @GetMapping("/byid")
    public List<Symptom> getSymptomByid(@RequestParam Integer id){
        return iSymptomService.list(new LambdaQueryWrapper<Symptom>().eq(Symptom::getId,id));
    }
    @ApiOperation(value = "查询当前病人所有病症")
    @GetMapping("/getPatient")
    public List<Patient> getPatient(@RequestParam Integer id){
        LambdaQueryWrapper<Rs> lam  = new LambdaQueryWrapper<>();
        //第一个时间
        LocalDate localDate = LocalDate.now();
        lam.eq(Rs::getPid,id);
        List<Rs> rsList = iRsService.list(lam);
        for (Rs rs : rsList){
            Integer id1 = rs.getId();
            //第二个时间
            LocalDate endDate = rs.getEndDate();
            if(localDate.isAfter(endDate)){
                iRsService.removeById(id1);
            }
        }
        return iPatientService.getSymptom(id);
    }

}
